package com.example.controller;

import com.example.domain.BaseEntity;
import com.example.domain.Result;
import com.example.domain.Scholarship;
import com.example.domain.Student;
import com.example.service.ScholarshipService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/mscholarship")
public class ManagerScholarshipController extends MyController {

    @Autowired
    private ScholarshipService scholarshipService;

    @PostMapping("/list")
    public Result list(Scholarship scholarship, HttpSession session) {

        BaseEntity baseEntity = (BaseEntity) session.getAttribute("login");
        if (baseEntity instanceof Student) {
            Student student = (Student) baseEntity;
            Long studentId = student.getStudentId();
            scholarship.setStudentId(studentId);
        }
        PageInfo<Map> pageInfo = this.scholarshipService.selectPageList(scholarship);
        return success(pageInfo);
    }

    @PostMapping("/pass/{id}")
    public Result pass(@PathVariable("id") Long id) {
        Scholarship scholarship = this.scholarshipService.selectById(id);
        if ( "初审中".equals(scholarship.getStatus())) {
            scholarship.setStatus("复审中");
        }
        scholarshipService.save(scholarship);
        return success();
    }

    @PostMapping("/failure/{id}")
    public Result failure(@PathVariable("id") Long id,String reason) {
        Scholarship scholarship = this.scholarshipService.selectById(id);
        scholarship.setStatus("审核失败");
        scholarship.setReason(reason);
        scholarshipService.save(scholarship);
        return success();
    }

    @PostMapping("/repass/{id}")
    public Result repass(@PathVariable("id") Long id, String level) {
        Scholarship scholarship = this.scholarshipService.selectById(id);
        if ( "复审中".equals(scholarship.getStatus())) {
            scholarship.setStatus("通过");
            scholarship.setLevel(level);
        }
        scholarshipService.save(scholarship);
        return success();
    }
}
